package com.ruoyi.procurement.service.impl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.procurement.domain.PsProcure;
import com.ruoyi.procurement.mapper.PsGoodsMapper;
import com.ruoyi.procurement.service.IPsGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.procurement.mapper.PsReportingLossesMapper;
import com.ruoyi.procurement.domain.PsReportingLosses;
import com.ruoyi.procurement.service.IPsReportingLossesService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 报损记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-11
 */
@Service
public class PsReportingLossesServiceImpl implements IPsReportingLossesService 
{
    @Autowired
    private PsReportingLossesMapper psReportingLossesMapper;
    @Autowired
    private IPsGoodsService iPsGoodsService;

    /**
     * 查询报损记录
     * 
     * @param id 报损记录主键
     * @return 报损记录
     */
    @Override
    public PsReportingLosses selectPsReportingLossesById(String id)
    {
        return psReportingLossesMapper.selectPsReportingLossesById(id);
    }

    /**
     * 查询报损记录列表
     * 
     * @param psReportingLosses 报损记录
     * @return 报损记录
     */
    @Override
    public List<PsReportingLosses> selectPsReportingLossesList(PsReportingLosses psReportingLosses)
    {
        return psReportingLossesMapper.selectPsReportingLossesList(psReportingLosses);
    }

    /**
     * 新增报损记录
     * 
     * @param psReportingLosses 报损记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertPsReportingLosses(PsReportingLosses psReportingLosses)
    {
        BigDecimal psGoodsNum = psReportingLosses.getPsGoodsNum();
        if (psGoodsNum==null || psGoodsNum.doubleValue()<=0){
            throw new ServiceException("请输入正确的报损数量！");
        }
        try {
            psReportingLosses.setCreateByName(SecurityUtils.getLoginUser().getUser().getNickName());
        } catch (Exception e) {
            psReportingLosses.setCreateByName("未知");
        }
        psReportingLosses.setCreateTime(DateUtils.getNowDate());
        iPsGoodsService.deduction(null,"报损",psReportingLosses,null,null);
        return psReportingLossesMapper.insert(psReportingLosses);
    }

    /**
     * 修改报损记录
     * 
     * @param psReportingLosses 报损记录
     * @return 结果
     */
    @Override
    public int updatePsReportingLosses(PsReportingLosses psReportingLosses)
    {
        psReportingLosses.setUpdateTime(DateUtils.getNowDate());
        return psReportingLossesMapper.updateById(psReportingLosses);
    }

    /**
     * 批量删除报损记录
     * 
     * @param ids 需要删除的报损记录主键
     * @return 结果
     */
    @Override
    public int deletePsReportingLossesByIds(String[] ids)
    {
        for (String id : ids) {
            PsReportingLosses psReportingLosses = selectPsReportingLossesById(id);

        }

        for (String id : ids) {
            PsReportingLosses psReportingLosses = selectPsReportingLossesById(id);
            iPsGoodsService.deduction(null,"报损回退",psReportingLosses,null,null);
        }
        return psReportingLossesMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除报损记录信息
     * 
     * @param id 报损记录主键
     * @return 结果
     */
    @Override
    public int deletePsReportingLossesById(String id)
    {
        return psReportingLossesMapper.deleteById(id);
    }

    @Override
    public void deleteByPsGoodsId(String goodsId) {
        psReportingLossesMapper.deleteByPsGoodsId(goodsId);
    }
}
